define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/goods/index',
                    add_url: 'goods/goods/add',
                    edit_url: 'goods/goods/edit',
                    del_url: 'goods/goods/del',
                    multi_url: 'goods/goods/multi',
                    table: 'goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: '#'},
                        // {field: 'admin_id', title: __('Admin_id')},
                        {field: 'goods_sn', title: __('Goods_sn')},
                        {field: 'goods_name', title: __('Goods_name')},
                        {field: 'data_country.name', title: '产地'},
                        {field: 'goods_price', title: __('Goods_price'),  formatter: Controller.api.formatter.combine},
                        // {field: 'goods_unit', title: __('Goods_unit')},
                        // {field: 'unit_code', title: __('Unit_code')},
                        {field: 'goods_weight', title: __('Goods_weight'), operate:'BETWEEN'},
                        {field: 'net_weight', title: __('Net_weight'), operate:'BETWEEN'},
                        // {field: 'brand_name', title: __('Brand_name')},
                        // {field: 'bar_code', title: __('Bar_code')},
                        {field: 'record_code', title: __('Record_code')},
                        // {field: 'record_hs_code', title: __('Record_hs_code')},
                        // {field: 'record_price', title: __('Record_price')},
                        // {field: 'record_time', title: __('Record_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'goods_spec', title: __('Goods_spec')},
                        // {field: 'goods_desc', title: __('Goods_desc')},
                        // {field: 'created_time', title: __('Created_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'goods_status', title: __('Goods_status'), searchList: {"0":__('Goods_status 0'),"1":__('Goods_status 1'),"2":__('Goods_status 2')}, formatter: Table.api.formatter.status},
                        {field: 'record_status', title: __('Record_status'), searchList: {"undeclared":__('Record_status undeclared'),"submitted":__('Record_status submitted'),"processing":__('Record_status processing'),"success":__('Record_status success'),"failure":__('Record_status failure'),"except":__('Record_status except')}, formatter: Table.api.formatter.status},
                        // {field: 'country_code', title: __('Country_code')},
                        {field: 'admin.nickname', title: __('Admin.nickname')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                combine: function (value, row, index) {
                    if (row.data_unit.name != null) {
                        return '' + row.goods_price + '/' + row.data_unit.name;
                    }else{
                        return '' + row.goods_price;
                    }
                },
            },
        }
    };
    return Controller;
});